import { Controller } from "../../mvc/core/Controller";
import { INewYearRedPackModule } from "../../../modules/interfaces/INewYearRedPackModule";
import { NewYearRedPackModule } from "../../../modules/NewYearRedPackModule";
import { INewYearRedPackRank } from "../../../modules/interfaces/INewYearRedPackRank";
import { NewYearRedPackRankModule } from "../../../modules/NewYearRedPackRankModule";
import { NewYearRedPackRankIcon } from "../view/newYearRedPack/NewYearRedPackRankIcon";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SRedEnvelopeRewardInfos } from "../../../Message/Game/SRedEnvelopeRewardInfos";
import { SHandselFlowerBack } from "../../../Message/Public/SHandselFlowerBack";
import { ItemData } from "../resource/info/ItemData";
import { ItemsUtil } from "../view/shortcuts/item/ItemsUtil";
import { SRedEnvelopeRankInfo } from "../../../Message/Game/SRedEnvelopeRankInfo";
import { SReceiveRedEnvelope } from "../../../Message/Game/SReceiveRedEnvelope";
import { DataEvent } from "../events/DataEvent";
import { SendRedPackData } from "../view/newYearRedPack/SendRedPackData";
import { GameProxy } from "../mvc/GameProxy";
import { SRedEnvelopeActiveInfo } from "../../../Message/Game/SRedEnvelopeActiveInfo";
//class NewYearRedPackController
    
    export  class NewYearRedPackController extends Controller
    {
       

        /* internal  */get module(): INewYearRedPackModule
        {
            if (!this._module) 
            {
                this._module = new NewYearRedPackModule();
            }
            return this._module;
        }

        /* internal  */get rankPanel(): INewYearRedPackRank
        {
            if (!this._rankPanel) 
            {
                this._rankPanel = new NewYearRedPackRankModule();
            }
            return this._rankPanel;
        }

        /* internal  */get rankIcon(): NewYearRedPackRankIcon
        {
            if (!this._rankIcon) 
            {
                this._rankIcon = new NewYearRedPackRankIcon();
                this._rankIcon.updateIconInfo();
            }
            return this._rankIcon;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRedEnvelopeRankInfo, this.onRedPackRankInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRedEnvelopeActiveInfo, this.activeIsOpen);
            NetDispatcher.addCmdListener(ServerCommand.FriendOnlineStatus, this.onFriendOnlienStatus);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRedEnvelopeActiveRewardInfo, this.onRewardContent);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateHandselFlowerBack, this.onHandselFlowerBack);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateRedEnvelopeInfo, this.receiveRedPack);
            Dispatcher.addEventListener(EventName.UseNewYearRedPack, this.useRedPackOpenWin);
            Dispatcher.addEventListener(EventName.ClickRedPackRankIcon, this.openRankPanel);
            Dispatcher.addEventListener(EventName.ClickTxtOpenRankView, this.openRankPanel);
            Dispatcher.addEventListener(EventName.OnSendRedPack, this.onSendRedPack);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
            return;
        }

        /* internal  */onRewardContent(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRedEnvelopeRewardInfos;
            cache.active.redPackRankReward = loc1.rewardInfo;
            return;
        }

        /* internal  */onHandselFlowerBack(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SHandselFlowerBack;
            var loc2=new ItemData(loc1.code);
            if (!ItemsUtil.isRedPack(loc2)) 
            {
                return;
            }
            if (this.module.isHide) 
            {
                this.module.show();
            }
            this.module.sendRedPackSuccess(loc1);
            return;
        }

        /* internal  */onRedPackRankInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRedEnvelopeRankInfo;
            if (!loc1) 
            {
                return;
            }
            cache.active.rankInfo = loc1.rankInfo;
            if (!this.rankPanel.isHide) 
            {
                this.rankPanel.updateRank(loc1.rankInfo);
            }
            return;
        }

        /* internal  */receiveRedPack(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SReceiveRedEnvelope;
            if (this.module.isHide) 
            {
                this.module.show();
            }
            this.module.receiveRedPackInfo(loc1);
            return;
        }

        /* internal  */onFriendOnlienStatus(arg1: Object): void
        {
            if (!this.module.isHide) 
            {
                this.module.updateFriendList();
            }
            return;
        }

        /* internal  */onSendRedPack(arg1: DataEvent): void
        {
            var loc1=arg1.data as SendRedPackData;
            GameProxy.redPack.send(loc1.fromPlayerId, loc1.toPlayerName, loc1.type, loc1.uid, loc1.autoBuy, loc1.code, loc1.goldNum);
            return;
        }

        protected onRoleLevelUpdateHandler(arg1: Object): void
        {
            if (cache.role.entityInfo.level >= 30 && cache.active.newRedPackIsOpen) 
            {
                if (this.rankIcon.isHide) 
                {
                    this.rankIcon.show();
                }
            }
            return;
        }

        /* internal  */openRankPanel(arg1: DataEvent): void
        {
            if (this.rankPanel.isHide) 
            {
                this.rankPanel.show();
                if (cache.active.rankInfo.length == 0) 
                {
                    GameProxy.active.getRedPackRankInfo();
                }
                else 
                {
                    this.rankPanel.updateRank(cache.active.rankInfo);
                }
            }
            else 
            {
                this.rankPanel.hide();
            }
            return;
        }

        /* internal  */activeIsOpen(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRedEnvelopeActiveInfo;
            if (loc1.isStart && cache.role.roleInfo.level >= 30) 
            {
                this.rankIcon.show();
            }
            else 
            {
                this.rankIcon.hide();
                if (!this.module.isHide) 
                {
                    this.module.hide();
                }
                if (!this.rankPanel.isHide) 
                {
                    this.rankPanel.hide();
                }
            }
            cache.active.newRedPackIsOpen = loc1.isStart;
            return;
        }

        /* internal  */useRedPackOpenWin(arg1: DataEvent): void
        {
            if (this.module.isHide) 
            {
                this.module.show();
            }
            return;
        }

        private /* var */_module: INewYearRedPackModule;

        private /* var */_rankIcon: NewYearRedPackRankIcon;

        private /* var */_rankPanel: INewYearRedPackRank;
    }
